import 'package:flutter/cupertino.dart';
import 'package:video_player/video_player.dart';

import '../../../assets_tool/app_colors.dart';

class ShortVideoLoading extends StatefulWidget {
  final VideoPlayerController? controller;

  const ShortVideoLoading({super.key, this.controller});

  @override
  State<StatefulWidget> createState() {
    return _ShortVideoLoadingState();
  }
}

class _ShortVideoLoadingState extends State<ShortVideoLoading> {
  @override
  void initState() {
    super.initState();
    widget.controller?.addListener(_listenCallback);
  }

  @override
  void didUpdateWidget(covariant ShortVideoLoading oldWidget) {
    if (widget.controller != oldWidget.controller) {
      oldWidget.controller?.removeListener(_listenCallback);
      widget.controller?.addListener(_listenCallback);
    }
    super.didUpdateWidget(oldWidget);
  }

  void _listenCallback() {
    if (mounted) {
      setState(() {});
    }
  }

  @override
  void dispose() {
    super.dispose();
    widget.controller?.removeListener(_listenCallback);
  }

  @override
  Widget build(BuildContext context) {
    if (widget.controller == null ||
        widget.controller?.value.isInitialized != true ||
        widget.controller?.value.isBuffering == true) {
      return const Center(
        child: CupertinoActivityIndicator(
          color: AppColors.actionRed,
          radius: 15,
        ),
      );
    } else {
      return const SizedBox();
    }
  }
}
